package com.cgbl.cg.ui.classification

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.fastjson.JSON
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.ClassificationImgAdapter
import com.cgbl.cg.adapter.ClassificationTabAdapter
import com.cgbl.cg.databinding.AppFragmentClassificationTabBinding
import com.cgbl.cg.vm.classification.ClassificationTabViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.util.arouter.ARouterUtil
import com.robot.base.view.CgTextView
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter

class ClassificationTabFragment :
    GLBaseFragment<AppFragmentClassificationTabBinding, ClassificationTabViewModel>() {

    var typeTab = ""
    private lateinit var tagAdapter: TagAdapter<CourseAllTypeBean>
    private lateinit var classificationTabAdapter: ClassificationTabAdapter
    private lateinit var classificationImgAdapter: ClassificationImgAdapter

    companion object {
        fun newInstance(typeTab: String): ClassificationTabFragment {
            val fragment = ClassificationTabFragment()
            val bundle = Bundle()
            bundle.putString("typeTab", typeTab)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initParam() {
        super.initParam()
        typeTab = arguments?.getString("typeTab") ?: ""

    }

    override fun initContentView(
        inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_classification_tab

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        viewModel.typeTab.set(typeTab)
        initAdapter()
    }

    override fun initData() {
        super.initData()
        viewModel.initData(typeTab)
    }

    private fun initAdapter() {
        tagAdapter = object : TagAdapter<CourseAllTypeBean>(viewModel.clickList) {
            override fun getView(parent: FlowLayout, position: Int, s: CourseAllTypeBean?): View {
                val flowLayout = LayoutInflater.from(requireContext()).inflate(
                    R.layout.app_flow_classification, binding.flowLayout, false
                )
                val tvContent = flowLayout.findViewById<CgTextView>(R.id.item_tv_2)
                tvContent.text = s?.name
                return flowLayout
            }
        }

        binding.flowLayout.adapter = tagAdapter
        binding.flowLayout.setOnTagClickListener { view, position, parent ->
            when (typeTab) {
                "0", "1" -> {
                    ARouterUtil.goToActivityWithBundle(AppConstants.Router.COURSE.A_COURSE_MENU,
                        Bundle().apply {
                            putString(
                                AppConstants.BundleKey.CLASSIFICATION_DATA,
                                JSON.toJSONString(classificationTabAdapter.data)
                            )
                            putInt(
                                AppConstants.BundleKey.CLASSIFICATION_FIRST_POSITION,
                                classificationTabAdapter.position
                            )
                            putInt(
                                AppConstants.BundleKey.CLASSIFICATION_SECOND_POSITION, position
                            )
                        })
                }
                "2" -> {
                    ARouterUtil.goToActivity(AppConstants.Router.COLLECTION.A_DISCOVERY_COLLECTION)
                }
            }
            return@setOnTagClickListener true
        }

        classificationTabAdapter = ClassificationTabAdapter()
        binding.recyclerViewTab.apply {
            adapter = classificationTabAdapter
        }

        classificationTabAdapter.setOnItemClickListener { adapter, view, position ->
            viewModel.clickList.clear()
            classificationTabAdapter.data[position].childrenList?.let { it1 ->
                viewModel.clickList.addAll(
                    it1
                )
            }
            tagAdapter.notifyDataChanged()
        }

        classificationImgAdapter = ClassificationImgAdapter()
        binding.mRvImg.apply {
            adapter = classificationImgAdapter
        }
        classificationImgAdapter.setNewInstance(
            arrayListOf(
                "虚拟仿真", "游戏开发", "硬件交互开发", "可视化开发", "影视动画"
            )
        )
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.dataEvent.observe(this) {
            classificationTabAdapter.setNewInstance(it)
            viewModel.clickList.clear()
            it[0].childrenList?.let { it1 -> viewModel.clickList.addAll(it1) }
            tagAdapter.notifyDataChanged()
        }
    }
}